package com.java.netty.common.codec;

import io.netty.buffer.ByteBuf;

public class VncPacket {

   private VncPackageContent content;
   private int pkgSize;
   private boolean dirty;
   private int crc32;

   public VncPackageContent getContent() {
      return content;
   }

   public void setContent(VncPackageContent content) {
      this.content = content;
   }

   public int getPkgSize() {
      return pkgSize;
   }

   public void setPkgSize(int pkgSize) {
      this.pkgSize = pkgSize;
   }

   public boolean isDirty() {
      return dirty;
   }

   public void setDirty(boolean dirty) {
      this.dirty = dirty;
   }

   public int getCrc32() {
      return crc32;
   }

   public void setCrc32(int crc32) {
      this.crc32 = crc32;
   }

   public ByteBuf encode(final ByteBuf out) throws Exception {
      out.writeByte(pkgSize);
      out.writeBytes(content.getBy());
      out.writeByte(crc32);
      return out;
   }

   public VncPacket decode(final ByteBuf in) throws Exception {
      pkgSize = in.readInt();
      System.out.println("-----++++++++++++++++++" + pkgSize);
      return this;
   }

   public static VncPacket decodePacket(VncPacket packetBase, ByteBuf in) {

      //读取包长度的4个字节
      packetBase.pkgSize = Packet.byte2Int(in.getByte(0), in.getByte(3));
      packetBase.crc32 = (packetBase.pkgSize - 4 << 24) | (packetBase.pkgSize - 3 << 16) | (packetBase.pkgSize - 2 << 8) | (packetBase.pkgSize - 1);
      packetBase.dirty = false;
      byte content = in.getByte(4);
      //      packetBase.content.setTarget();

      //        packetBase.cmd = in.readByte();
      //        packetBase.cmd = packetBase.cmd >> 2;
      //        packetBase.protocolVer = in.readByte();
      //        packetBase.onCallTime = (short) byte2Int(in.readByte(), in.readByte());
      //        packetBase.msgLen = byte2Int(in.readByte(), in.readByte());
      //        if (packetBase.msgLen < PACKET_DATA_MAX_LEN) {
      //            in.readBytes(packetBase.data = new byte[packetBase.msgLen]);
      //        } else {
      //            return null;
      //        }
      return packetBase;
   }
}
